package me.pcqpcq.android.aclean;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.pcqpcq.android.Constant;
import me.pcqpcq.android.adapter.SimpleCleanExpanListAdapter;
import me.pcqpcq.android.data.CleanListData;
import me.pcqpcq.android.mgr.ActivityMgr;
import me.pcqpcq.android.msg.MessageCentre;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ExpandableListView;

public class CleanRubbishActivity extends Activity implements OnClickListener {

	private List<Map<String, Object>> groupData = new ArrayList<Map<String, Object>>();
	private List<List<Map<String, Object>>> childData = new ArrayList<List<Map<String, Object>>>();
	private SimpleCleanExpanListAdapter cleanAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActivityMgr.setCurActivity(CleanRubbishActivity.this);
		setContentView(R.layout.act_clean_rubbish);
	}

	/**
	 * 构建清理列表
	 */
	private void buildExpandableList() {
		cleanAdapter =
				new SimpleCleanExpanListAdapter(CleanRubbishActivity.this, groupData,
						R.layout.exlistview_group_item, new String[] { "groupname" },
						new int[] { R.id.main_explist_group_textview }, childData, R.layout.listview_item,
						new String[] { "childname" }, new int[] { R.id.listview_textview });
		ExpandableListView cleanList = (ExpandableListView) findViewById(R.id.clean_rubbish_exlist);
		cleanList.setAdapter(cleanAdapter);
		// 异步加载数据
		new GetCleanDataTask().execute();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.btn_refresh:
			buildExpandableList();
			break;
			
		case R.id.btn_back:
			onBackPressed();
			break;

		default:
			break;
		}
	}

	/**
	 * 异步加载数据
	 * @author pcqpcq
	 *
	 */
	protected class GetCleanDataTask extends AsyncTask<Object, Void, Object> {

		@Override
		protected Object doInBackground(Object... params) {
			// 获取配置参数
			SharedPreferences cleanList =
					getSharedPreferences(Constant.PREFERENCE_CLEAN_LIST, Context.MODE_PRIVATE);
			boolean isCleanEmptyDir = cleanList.getBoolean(getString(R.string.key_dir_empty), false);
			boolean isCleanCacheDir = cleanList.getBoolean(getString(R.string.key_dir_cache), false);
			boolean isCleanRootFiles = cleanList.getBoolean(getString(R.string.key_file_root), false);
			boolean isCleanUselessDir = cleanList.getBoolean(getString(R.string.key_dir_useless), false);
			Integer cleanUselessDay =
					Integer.parseInt(cleanList.getString(getString(R.string.key_dir_useless_days), "0"));
			// 根据配置，扫描数据
			if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
				groupData.clear();
				childData.clear();

				// 扫描空目录
				if (isCleanEmptyDir) {
					MessageCentre.updateProgressDlg(getString(R.string.pd_cleanning_dir_empty), "");
					// 添加组数据
					List<Map<String, Object>> emptyChildList = new ArrayList<Map<String, Object>>();
					List<File> emptyFileList = CleanListData.getEmptyList(Environment.getExternalStorageDirectory());
					for (File file : emptyFileList) {
						Map<String, Object> emptyChildMap = new HashMap<String, Object>();
						if (file.isFile())
							emptyChildMap.put("childicon", (int) R.drawable.ic_file);
						else
							emptyChildMap.put("childicon", (int) R.drawable.ic_folder);
						emptyChildMap.put("childname", (String) file.getAbsolutePath());
						emptyChildMap.put("childfile", (File) file);
						emptyChildList.add(emptyChildMap);
					}
					// 添加组标题
					Map<String, Object> emptyGroup = new HashMap<String, Object>();
					emptyGroup.put("groupname",
							getString(R.string.preference_title_dir_empty).concat("：" + emptyChildList.size()));
					groupData.add(emptyGroup);
					// 将组添加到整个列表中
					childData.add(emptyChildList);
				}
				// 扫描缓存日志
				if (isCleanCacheDir) {
					MessageCentre.updateProgressDlg(getString(R.string.pd_cleanning_dir_cache), "");
					// 添加组数据
					List<Map<String, Object>> cacheChildList = new ArrayList<Map<String, Object>>();
					List<File> cacheFileList = CleanListData.getCacheList(Environment.getExternalStorageDirectory());
					for (File file : cacheFileList) {
						Map<String, Object> cacheChildMap = new HashMap<String, Object>();
						if (file.isFile())
							cacheChildMap.put("childicon", (int) R.drawable.ic_file);
						else
							cacheChildMap.put("childicon", (int) R.drawable.ic_folder);
						cacheChildMap.put("childname", (String) file.getAbsolutePath());
						cacheChildMap.put("childfile", (File) file);
						cacheChildList.add(cacheChildMap);
					}
					// 添加组标题
					Map<String, Object> cacheGroup = new HashMap<String, Object>();
					cacheGroup.put("groupname",
							getString(R.string.preference_title_dir_cache).concat("：" + cacheChildList.size()));
					groupData.add(cacheGroup);
					// 将组添加到整个列表中
					childData.add(cacheChildList);
				}
				// 扫描根目录的单文件
				if (isCleanRootFiles) {
					MessageCentre.updateProgressDlg(getString(R.string.pd_cleanning_file_root), "");
					// 添加组数据
					List<Map<String, Object>> rootFilesChildList = new ArrayList<Map<String, Object>>();
					List<File> rootFilesList = CleanListData.getListFile(Environment.getExternalStorageDirectory());
					for (File file : rootFilesList) {
						Map<String, Object> rootFilesChildMap = new HashMap<String, Object>();
						if (file.isFile())
							rootFilesChildMap.put("childicon", (int) R.drawable.ic_file);
						else
							rootFilesChildMap.put("childicon", (int) R.drawable.ic_folder);
						rootFilesChildMap.put("childname", (String) file.getAbsolutePath());
						rootFilesChildMap.put("childfile", (File) file);
						rootFilesChildList.add(rootFilesChildMap);
					}
					// 添加组标题
					Map<String, Object> rootFilesGroup = new HashMap<String, Object>();
					rootFilesGroup.put("groupname",
							getString(R.string.preference_title_file_root).concat("：" + rootFilesChildList.size()));
					groupData.add(rootFilesGroup);
					// 将组添加到整个列表中
					childData.add(rootFilesChildList);
				}
				// 扫描长期未使用目录
				if (isCleanUselessDir) {
					MessageCentre.updateProgressDlg(getString(R.string.pd_cleanning_dir_useless), "");
					// 添加组数据
					List<Map<String, Object>> uselessChildList = new ArrayList<Map<String, Object>>();
					List<File> uselessFileList =
							CleanListData.getUselessList(Environment.getExternalStorageDirectory(), cleanUselessDay);
					for (File file : uselessFileList) {
						Map<String, Object> uselessChildMap = new HashMap<String, Object>();
						if (file.isFile())
							uselessChildMap.put("childicon", (int) R.drawable.ic_file);
						else
							uselessChildMap.put("childicon", (int) R.drawable.ic_folder);
						uselessChildMap.put("childname", (String) file.getAbsolutePath());
						uselessChildMap.put("childfile", (File) file);
						uselessChildList.add(uselessChildMap);
					}
					// 添加组标题
					Map<String, Object> uselessGroup = new HashMap<String, Object>();
					uselessGroup.put(
							"groupname",
							getString(R.string.preference_title_parent_dir_useless).concat(
									"：" + uselessChildList.size()));
					groupData.add(uselessGroup);
					// 将组添加到整个列表中
					childData.add(uselessChildList);
				}
			}
			return null;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			MessageCentre.showProgressDlg(getString(R.string.pd_checking_cleanable), "");
		}

		@Override
		protected void onPostExecute(Object result) {
			super.onPostExecute(result);
			cleanAdapter.notifyDataSetChanged();
			MessageCentre.closeProgressDlg();
		}

	}

}